/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.utils;

import java.util.*;
import java.nio.*;


/**
 * Provides useful methods for debugging various types of data.
 */
public class DebugUtil
{

    /**
     * @param aData
     * @return String representation of this object
     */
    public static String toString( Object aData )
    {
        if (aData instanceof byte[])
            return toString( (byte[])aData );

        if (aData instanceof char[] )
            return toString( (char[])aData);

        else if ( aData instanceof String[])
            return toString( (String[])aData );

        else if ( aData instanceof Object[])
            return toString( (Object[])aData );

        else if ( aData instanceof ByteBuffer )
            return toString( (ByteBuffer)aData );



        else if ( aData instanceof Map )
            return toString( (Map)aData );

        else if ( aData instanceof Hashtable )
            return toString( (Hashtable)aData );

        else if ( aData == null )
            return "null";

        return aData.toString();
    }


    /**
     * @return String representation of a byte array object
     */
    public static String toString( byte[] aData )
    {
        String str = StringUtil.empty;
        str += "<byte[" + aData.length + "]>";

        for (byte anAData : aData) str += anAData + ",";
            //str += "(" + i + ")" + aData[i] + "\n";

        if (str.length() > 0 )
            str = str.substring( 0, str.length()-1 );

        str += "</byte[]>";

        return str;
    }


    /**
     * @return String representation of a char array object
     */
    public static String toString( char[] aData )
    {
        String str = StringUtil.empty;
        str += "<char[" + aData.length + "]>";

        for (char anAData : aData) str += anAData + ",";
            //str += "(" + i + ")" + aData[i] + "\n";

        if (str.length() > 0 )
            str = str.substring( 0, str.length()-1 );

        str += "</char[]>";

        return str;
    }


    /**
     * @param aArray
     * @return String representation of a string array
     */
    public static String toString( String[] aArray )
    {
        String str = StringUtil.empty;

        str += "<String[]>\n";
        for (int i = 0; i < aArray.length; i++)
             str += String.format( "  %d:(%s)=%s\n", i, aArray[i].getClass().toString(), aArray[i] );

        str += "</String[]>\n";

        return str;
    }


    /**
     * @param aArray
     * @return String representation of an object array
     */
    public static String toString( Object[] aArray )
    {
        String str = StringUtil.empty;

        str += "<Object[]>\n";
        for (int i = 0; i < aArray.length; i++)
             str += String.format( "  %d:(%s)=%s\n", i, aArray[i].getClass().toString(), aArray[i] );

        str += "</Object[]>\n";

        return str;
    }


    /**
     * @param aByteBuffer
     * @return String representation of a ByteBuffer object
     */
    public static String toString( ByteBuffer aByteBuffer )
    {
        String str = StringUtil.empty;
        str += "<ByteBuffer>\n";
        str += aByteBuffer.toString() + "\n";
        str += toString( aByteBuffer.array() ) + "\n";
        str += "</ByteBuffer>\n";

        return str;
    }




    /**
     * @param aMap
     * @return String representation of a Map object
     */
    public static String toString( Map aMap )
    {
        String str = StringUtil.empty;

        str += "<" + aMap.getClass().getName() + ">\n";
        for (Object key : aMap.keySet()) {
             Object val = aMap.get( key );
             String valAsStr = val.toString();
             if (val instanceof byte[]) {
                 valAsStr = toString((byte[])val);
             }
             str += "" + key + "=" + valAsStr + "\n";
        }
        str += "</" + aMap.getClass().getName() + ">\n";

        return str;
    }


    /**
     * @param aHashtable
     * @return String representation of a Hashtable object
     */
    public static String toString( Hashtable aHashtable )
    {
        String str = StringUtil.empty;

        str += "<" + aHashtable.getClass().getName() + ">\n";
        for (Object key : aHashtable.keySet())
             str += "(" + key + "):" + aHashtable.get( key ) + "\n";
        str += "</" + aHashtable.getClass().getName() + ">\n";
        return str;
    }


    /**
     * Prints an object
     *
     * @param aObject
     */
    public static void print( Object aObject )
    {
        System.out.println( toString(aObject) );
    }


    /**
     * Prints an object with a prefix infront
     *
     * @param aPrefix
     * @param aObject
     */
    public static void print( String aPrefix, Object aObject )
    {
        System.out.println( aPrefix + "::" + toString(aObject) );
    }


    /**
     * Prints a char array with a prefix infront
     *
     * @param aPrefix
     * @param aObject
     */
    public static void print( String aPrefix, char[] aObject )
    {
        System.out.println( aPrefix + "::" + toString(aObject) );
    }

}